package com.tm.mygsyplayer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.tm.mygsyplayer.R;

public class MyGsyPlayer extends StandardGSYVideoPlayer {

    private static final String TAG = "MyCustomGsyPlayer";
    ImageView ivStartPause;

    public MyGsyPlayer(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public MyGsyPlayer(Context context) {
        super(context);
    }

    public MyGsyPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getLayoutId() {
        return R.layout.empty_control_video;
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        ivStartPause = findViewById(R.id.iv_start_pause);

        if (ivStartPause != null) {
            ivStartPause.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startAndPause();
                }
            });
        }
    }

    private void startAndPause() {
        if (mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR) {
            if (isShowNetConfirm()) {
                showWifiDialog();
                return;
            }
            startButtonLogic();
            ivStartPause.setImageResource(R.drawable.exo_controls_pause);
            setViewShowState(ivStartPause, INVISIBLE);
        } else if (mCurrentState == CURRENT_STATE_PLAYING) {
            ivStartPause.setImageResource(R.drawable.exo_controls_play);
            setViewShowState(ivStartPause, VISIBLE);
            try {
                onVideoPause();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (mCurrentState == CURRENT_STATE_PAUSE) {
            ivStartPause.setImageResource(R.drawable.exo_controls_pause);
            setViewShowState(ivStartPause, INVISIBLE);
            if (!mHadPlay && !mStartAfterPrepared) {
                startAfterPrepared();
            }

            try {
                getGSYVideoManager().start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (mCurrentState == CURRENT_STATE_AUTO_COMPLETE) {
            startButtonLogic();
            ivStartPause.setImageResource(R.drawable.exo_controls_pause);
            setViewShowState(ivStartPause, INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        //super.onClick(v);
        if (v.getId() == R.id.thumb) {
//            if (!mThumbPlay) {
//                return;
//            }
            startAndPause();
        } else if (v.getId() == R.id.surface_container) {
            Log.i(TAG, "surfaceClick");
            startAndPause();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
//        return super.onTouch(v, event);
        Log.i(TAG, "onTouch");
        int id = v.getId();
        float x = event.getX();
        float y = event.getY();
        if (id == R.id.surface_container) {
            startAndPause();
        }
        return false;
    }

    @Override
    protected void onClickUiToggle() {
        super.onClickUiToggle();
        startAndPause();
    }

    @Override
    protected void changeUiToNormal() {
        super.changeUiToNormal();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);
        setViewShowState(ivStartPause, INVISIBLE);
        //setViewShowState(mStartButton, VISIBLE);
        //getCurrentState()
    }
//
//    @Override
//    protected void changeUiToPauseShow() {
//        super.changeUiToPauseShow();
//    }

    @Override
    protected void changeUiToPlayingBufferingShow() {
        super.changeUiToPlayingBufferingShow();
        setViewShowState(ivStartPause, INVISIBLE);
    }

    @Override
    protected void changeUiToPlayingShow() {
        super.changeUiToPlayingShow();
        setViewShowState(ivStartPause, INVISIBLE);
    }

    //    @Override
//    protected void changeUiToCompleteShow() {
//        super.changeUiToCompleteShow();
//    }
//
//    @Override
//    protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
//        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY);
//        //不给触摸快进，如果需要，屏蔽下方代码即可
//        mChangePosition = false;
//
//        //不给触摸音量，如果需要，屏蔽下方代码即可
//        mChangeVolume = false;
//
//        //不给触摸亮度，如果需要，屏蔽下方代码即可
//        mBrightness = false;
//
//    }
//
    @Override
    protected void touchDoubleUp() {
//        super.touchDoubleUp();
        //不需要双击暂停
    }
//
//    @Override
//    protected void touchSurfaceUp() {
//        super.touchSurfaceUp();
//    }
//
//    @Override
//    protected void touchSurfaceMove(float deltaX, float deltaY, float y) {
//        super.touchSurfaceMove(deltaX, deltaY, y);
//    }
//
//    @Override
//    protected void touchSurfaceDown(float x, float y) {
//        super.touchSurfaceDown(x, y);
//    }

}
